package com.jwsoft.manager.core.gateway.controller.common;

import cn.hutool.extra.servlet.ServletUtil;
import com.alibaba.fastjson.JSONArray;
import com.bifang.engine.assistant.core.domain.AppResponse;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.protect.core.annotation.NoSignCrypto;
import com.bifang.module.base.common.enums.ErrorEnum;
import com.bifang.module.base.core.utils.ServiceRequestUtil;
import com.jwsoft.manager.common.vo.dygx.*;
import com.jwsoft.manager.common.vo.eduHouse.EduHouseVO;
import com.jwsoft.manager.common.vo.eduHouse.HouseInterfaceVO;
import com.jwsoft.manager.common.vo.eduStudentEnrollPre.EduStudentEnrollPreKeyVO;
import com.jwsoft.manager.core.dao.model.EduStudentEnrollPre;
import com.jwsoft.manager.core.dao.service.EduStudentEnrollPreService;
import com.jwsoft.manager.core.dao.service.EduStudentEnrollService;
import com.jwsoft.manager.core.integration.common.DygxIntegration;
import com.jwsoft.manager.core.integration.individuation.impl.DyIndividuationIntegrationImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping(value = "/doc/api")
@Tag(name = "东阳大数据共享接口", description = "东阳大数据共享接口")
public class DygxController {
    @Autowired
    DygxIntegration dygxIntegration;
    @Autowired
    DyIndividuationIntegrationImpl dyIndividuationIntegration;
    @Autowired
    EduStudentEnrollPreService eduStudentEnrollPreService;
    @GetMapping(value = "/DYGX0001")
    @Operation(summary = "获取token信息",description = "获取token信息")
    public AppResponse<String> getToken(){
        chekLogin();
        return AppResponse.successData(dygxIntegration.getToken());
    }
    @GetMapping(value = "/IRSGX0001")
    @Operation(summary = "获取IRS的token信息",description = "获取IRS的token信息")
    public AppResponse<String> refreshTokenByKeyIrs(){
        return AppResponse.successData(dygxIntegration.refreshTokenByKeyIrs(null,null));
    }
//    @GetMapping(value = "/dygx0002")
//    @ApiOperation(value = "中小学学籍信息", notes = "中小学学籍信息")
//    public AppResponse<JSONArray> getStudentStatus(@ApiParam("证件号码") @RequestParam("zjbh") String zjbh){
//        chekLogin();
//        return AppResponse.successData(dygxIntegration.getStudentStatus(zjbh));
//    }
    @GetMapping(value = "/DYGX0003")
    @Operation(summary = "东阳市不动产登记系统提供查询服务(根据姓名身份证查询)",description = "东阳市不动产登记系统提供查询服务(根据姓名身份证查询)")
    @NoSignCrypto
    public AppResponse<JSONArray> getPersonHouse(@Param("证件号码") @RequestParam("zjh") String zjh,
                                                 @Param("姓名") @RequestParam("name") String name,  HttpServletRequest request){
        chekLogin();
        String ip = ServletUtil.getClientIP(request);
        return AppResponse.successData(dygxIntegration.getPersonHouse(name,zjh,ip));
    }
    @GetMapping(value = "/DYGX0004")
    @Operation(summary = "住建局房地产网签数据", description = "住建局房地产网签数据")
    @NoSignCrypto
    public AppResponse<List<WqVO>> getHouseOnline(@Param("证件号码") @RequestParam("zjhm") String zjhm){
        chekLogin();
        return AppResponse.successData(dygxIntegration.getHouseOnline(zjhm));
    }
    @GetMapping(value = "/DYGX0006")
    @Operation(summary = "个人参保查询（东阳）", description = "个人参保查询（东阳）")
    @NoSignCrypto
    public AppResponse<List<CbVO>> getGrcbDy(@Param("证件号码") @RequestParam("aac147") String aac147,  HttpServletRequest request){
        chekLogin();
        String ip = ServletUtil.getClientIP(request);
        return AppResponse.successData(dygxIntegration.getGrcbDy(aac147,ip));
    }
//    @GetMapping(value = "/dygx0009")
//    @ApiOperation(value = "个人参保查询（省）", notes = "个人参保查询（省）")
//    public AppResponse<JSONArray> getGrcb(@ApiParam("证件号码") @RequestParam("aac002") String aac002,@ApiParam("姓名") @RequestParam("aac003") String aac003){
//        chekLogin();
//        return AppResponse.successData(dygxIntegration.getGrcb(aac002,aac003));
//    }
//    @GetMapping(value = "/dygx0007")
//    @ApiOperation(value = "市场监督管理局市场主体信息（东阳）", notes = "市场监督管理局市场主体信息（东阳）")
//    public AppResponse<JSONArray> getOrgInfo(@ApiParam("统一社会信用代码") @RequestParam("uniscid") String uniscid){
//        chekLogin();
//        return AppResponse.successData(dygxIntegration.getOrgInfo(uniscid));
//    }
    @GetMapping(value = "/DYGX0005")
    @Operation(summary = "省公安厅居民户口簿(家庭)", description = "省公安厅居民户口簿(家庭)")
    @NoSignCrypto
    public AppResponse<JSONArray> getFamilyInfo(@Param("身份证号码") @RequestParam("czrkgmsfhm") String czrkgmsfhm){
        chekLogin();
        return AppResponse.successData(dygxIntegration.getFamilyInfo(czrkgmsfhm));
    }
    @GetMapping(value = "/IRSGX0003")
    @Operation(summary = "IRS省公安厅居民户口簿(家庭)", description = "省公安厅居民户口簿(家庭)")
    @NoSignCrypto
    public AppResponse<JSONArray> getFamilyInfoIrs(@Param("身份证号码") @RequestParam("czrkgmsfhm") String czrkgmsfhm,  HttpServletRequest request){
        chekLogin();
        String ip = ServletUtil.getClientIP(request);
        return AppResponse.successData(dygxIntegration.getFamilyInfoIrs(czrkgmsfhm,ip));
    }

    @GetMapping(value = "/IRSGX0005")
    @Operation(summary = "IRS省公安厅居住证", description = "IRS省公安厅居住证")
    @NoSignCrypto
    public AppResponse<JSONArray> getFamilyInfoIrs(@Param("身份证号码") @RequestParam("sfzh") String sfzh, @Param("姓名") @RequestParam("xm") String xm,  HttpServletRequest request) {
        chekLogin();
        String ip = ServletUtil.getClientIP(request);
        return AppResponse.successData(dygxIntegration.getJzzIrs(sfzh, xm,ip));
    }
    @GetMapping(value = "/dygx0010")
    @Operation(summary = "户籍信息（东阳）", description = "户籍信息（东阳）")
    @NoSignCrypto
    public AppResponse<List<DyHouseholdVO>> getDyHousehold(@Param("身份证号码") @RequestParam("gmsfhm") String gmsfhm,  HttpServletRequest request){
        chekLogin();
        String ip = ServletUtil.getClientIP(request);
        return AppResponse.successData(dygxIntegration.getDyHjInfo(gmsfhm,ip));
    }
    @GetMapping(value = "/DYGX0011")
    @Operation(summary = "居住证信息（东阳）", description = "居住证信息（东阳）")
    @NoSignCrypto
    public AppResponse<List<DyJzzVO>> getDyJzz(@Param("身份证号码") @RequestParam("sfzh") String sfzh,  HttpServletRequest request){
        chekLogin();
        String ip = ServletUtil.getClientIP(request);
        return AppResponse.successData(dygxIntegration.getDyJzz(sfzh,ip));
    }
    @PostMapping(value = "/DYGX0012")
    @Operation(summary = "出生证明(省)", description = "出生证明(省)")
    @NoSignCrypto
    public AppResponse<List<CszmVO>> getCszm(@RequestBody CszmQueryVO cszmQueryVO){
        chekLogin();
        return AppResponse.successData(dygxIntegration.getCszm(cszmQueryVO));
    }
    @PostMapping(value = "/DYGX0013")
    @Operation(summary = "法人登记基本信息（省）", description = "法人登记基本信息（省）")
    @NoSignCrypto
    public AppResponse<List<QyVO>> getQy(@RequestBody QyQueryVO qyQueryVO){
        chekLogin();
        return AppResponse.successData(dygxIntegration.getQy(qyQueryVO));
    }

    @GetMapping(value = "/DYGX0015")
    @Operation(summary = "根据身份证提供该人员社保累计缴费月数(东阳)", description = "根据身份证提供该人员社保累计缴费月数(东阳)")
    @NoSignCrypto
    public AppResponse<Integer> getCbJfys(@Param("身份证号码") @RequestParam("aac147") String aac147,  HttpServletRequest request) {
        chekLogin();
        String ip = ServletUtil.getClientIP(request);
        return AppResponse.successData(dygxIntegration.getCbJfys(aac147,ip));
    }
    @GetMapping(value = "/DYJHGX0003")
    @Operation(summary = "金华大数据共享-通过合同编号，行政区划查询金华市二手房合同查询接口", description = "金华大数据共享-通过合同编号，行政区划查询金华市二手房合同查询接口")
    @NoSignCrypto
    public AppResponse<List<WqVO>> getgfht2(@Param("身份证号码") @RequestParam("sfzh") String sfzh,@Param("合同编号") @RequestParam("htbh") String htbh,@Param("合同备案号") @RequestParam("htbah") String htbah,  HttpServletRequest request) {
        String ip = ServletUtil.getClientIP(request);
        return AppResponse.successData(dygxIntegration.getHouseOnlineJH2(sfzh,htbh,htbah,null,ip));
    }
    @GetMapping(value = "/DYJHGX0004")
    @Operation(summary = "金华大数据共享-通过合同编号，行政区划查询金华市一手房合同查询接口", description = "金华大数据共享-通过合同编号，行政区划查询金华市二手房合同查询接口")
    @NoSignCrypto
    public AppResponse<List<WqVO>> getgfht1(@Param("身份证号码") @RequestParam("sfzh") String sfzh,@Param("合同编号") @RequestParam("htbh") String htbh,@Param("合同备案号") @RequestParam("htbah") String htbah,  HttpServletRequest request) {
        String ip = ServletUtil.getClientIP(request);
        return AppResponse.successData(dygxIntegration.getHouseOnlineJH1(sfzh,htbh,htbah,null,ip));
    }
    @GetMapping(value = "/DYJHGX0005")
    @Operation(summary = "金华大数据共享-金华市房产信息验证接口", description = "金华大数据共享-金华市房产信息验证接口")
    @NoSignCrypto
    public AppResponse<List<HouseInterfaceVO>> getHouseByBdczh(@Param("不动产证号") @RequestParam("bdcqzh") String bdcqzh, @Param("权利人名称") @RequestParam("qlrmc") String qlrmc,  HttpServletRequest request) {
        String ip = ServletUtil.getClientIP(request);
        return AppResponse.successData(dygxIntegration.getHouseByBdczh(bdcqzh,qlrmc,ip));
    }
    @PostMapping(value = "/TEST0001")
    @Operation(summary = "测试单个预测", description = "测试单个预测")
    public AppResponse getQy(@RequestBody EduStudentEnrollPreKeyVO eduStudentEnrollPreKeyVO){
        EduStudentEnrollPre eduStudentEnrollPre=eduStudentEnrollPreService.getById(eduStudentEnrollPreKeyVO.getStudentEnrollPreId());
        return AppResponse.successData(dyIndividuationIntegration.createCalculate(eduStudentEnrollPre));
    }
    private void chekLogin(){
        ServletRequestAttributes attributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        HttpServletResponse response = attributes.getResponse();
        String acccessToken = ServiceRequestUtil.getAccessToken(request, response);
        if (StringUtils.isBlank(acccessToken)){
            throw new AppException(ErrorEnum.SYSTEM_ERROR_NO_LONIN);
        }
    }
}
